package net.gxf.findinfo;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class MainUI extends JFrame {

	private static final long serialVersionUID = 1L;
	private FindInfo findInfo = new FindInfo();
	private JFileChooser fileChooser = new JFileChooser();

	public MainUI() {
		this.setSize(500, 150);
		this.setLocationRelativeTo(null);
		this.setTitle("Debug");
		// this.setUndecorated(true);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setVisible(true);
	}

	private void run() {
		final JTextField datasrc = new JTextField(30);
		final JTextField datades = new JTextField(30);
		JButton srcbtn = new JButton("浏览...");
		final JButton desbtn = new JButton("浏览...");
		Box box1 = Box.createHorizontalBox();
		box1.add(new JLabel("选择数据："));
		box1.add(datasrc);
		box1.add(srcbtn);

		Box box2 = Box.createHorizontalBox();
		box2.add(new JLabel("输出数据："));
		box2.add(datades);
		box2.add(desbtn);

		Box box = Box.createVerticalBox();
		box.add(Box.createVerticalStrut(10));
		box.add(box1);
		box.add(Box.createVerticalStrut(10));
		box.add(box2);

		srcbtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
			//	JFileChooser fileChooser = new JFileChooser();
				fileChooser.setDialogTitle("选择数据");

				int returnVal = fileChooser.showOpenDialog(MainUI.this);
				if (returnVal == JFileChooser.APPROVE_OPTION) {
					final String path = fileChooser.getSelectedFile()
							.getAbsolutePath();
					datasrc.setText(path);
					System.out.println("You chose to open this file: " + path);
	/*				new Thread() {

						@Override
						public void run() {
							findInfo.deal(path);
						}
					}.run();*/
				}

			}
		});
		desbtn.addActionListener(new ActionListener() {

		

			@Override
			public void actionPerformed(ActionEvent arg0) {
				fileChooser.setDialogTitle("导出数据");
				File file = new File("C:/data1.txt");
				System.out.println("ok");
				try {
					file.createNewFile();
				} catch (IOException e) {
					e.printStackTrace();
				}

				fileChooser.setCurrentDirectory(new File("C:/"));
				fileChooser.setSelectedFile(file);
				int returnVal = fileChooser.showSaveDialog(MainUI.this);
				if (returnVal == JFileChooser.APPROVE_OPTION) {
					String path = fileChooser.getSelectedFile()
							.getAbsolutePath();
					System.out.println("You chose to open this file: " + path);
					datades.setText(path);
				}
			}
		});

		JButton okButton = new JButton("确定");
		JButton cancelButton = new JButton("关闭");
		JPanel bottomPanel = new JPanel();
		bottomPanel.add(okButton);
		bottomPanel.add(cancelButton);
		okButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				findInfo.deal(datasrc.getText().trim());
				findInfo.export(datades.getText().trim());
				findInfo.clear();
				JOptionPane.showMessageDialog(MainUI.this, "数据已经生成",
						"注意", JOptionPane.INFORMATION_MESSAGE);

			}
		});
		//关闭事件处理
		cancelButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				MainUI.this.dispose();
			}
		});
		this.add(box, BorderLayout.NORTH);
		this.add(bottomPanel, BorderLayout.SOUTH);
		this.validate();

	}

	public static void main(String[] args) throws ClassNotFoundException,
			InstantiationException, IllegalAccessException,
			UnsupportedLookAndFeelException {
		UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		MainUI mainUI = new MainUI();
		mainUI.run();
	}

}
